package lar.system.engine;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.google.ads.doubleclick.DfpInterstitialAd;

import lar.game.gamestate.MainMenu;
import lar.game.gamestate.PauseMenu;
import lar.system.sound.Music;
import lar.system.sound.Sound;
import lar.system.R;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainProgram extends Activity 
{
	public static UI glSurfaceView=null;
	public static Activity currentActivity=null;
	public static GameLoop gameLoop=null;
	public static Thread gameLoopThread=null;
	public static AdView adView;
	public static DfpInterstitialAd adView2;
	public static boolean ads = false;
	public static final int NETCODE_VERSION = 10;
	public static final String VERSION_STRING = "v1.2.6"; 
	public static final boolean smartRender = true;
	public static boolean tracers = true;
	
    public void onCreate(Bundle savedInstanceState) 
    {
    	Log.d("R", "Activity Created.");
        super.onCreate(savedInstanceState);
        
         
        boolean isNewProcess = gameLoop == null;
        currentActivity = this;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    	getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
    	if(isNewProcess) gameLoop = new GameLoop();
         
        if(isNewProcess)
        {
        	gameLoopThread = new Thread(gameLoop);
        	gameLoopThread.start();
        }
        glSurfaceView = new UI(this);
        //setContentView(glSurfaceView);
         
        RelativeLayout layout = new RelativeLayout(this);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
        	    RelativeLayout.LayoutParams.WRAP_CONTENT, 
        	    RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);
        
        layout.addView(glSurfaceView); 
        //layout.addView(adView, params);
        
        setContentView(layout);
        
        //AdRequest adrequest = new AdRequest();
        //adrequest.addTestDevice(AdRequest.TEST_EMULATOR);
        //adrequest.addTestDevice("5C2B5523E466A622A62C7B86CB8B4920"); //nexus
        //adrequest.addTestDevice("90EEBCE577617E637E9BB39B4D927C79"); //asus
        //adView.loadAd(adrequest);
        //if(!MainProgram.ads) MainProgram.adView.setVisibility(View.GONE);
        
        //music = Sound.playSound(R.raw.tribes10);
        //music.setVolume(0.1f, 0.1f);
        if(MainProgram.currentActivity.getPreferences(Context.MODE_PRIVATE).getBoolean("SOUND", true)) Sound.enableSound();
		else Sound.disableSound();
        
        if(MainProgram.currentActivity.getPreferences(Context.MODE_PRIVATE).getBoolean("TRACERS", true)) tracers = true;
		else tracers = false;
		
    }
   
    @Override
    public void onDestroy() 
    {
    	if (adView != null) 
    	{
    		adView.removeAllViews();
    		adView.destroy();
    	}
    	if(gameLoop.getState() != null) gameLoop.getState().onDestroy();
    	super.onDestroy();
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event){
        
    	int keyCode = event.getKeyCode();
    	if(event.getAction() == KeyEvent.ACTION_UP) {
            return super.dispatchKeyEvent(event);
        }

        if (keyCode == KeyEvent.KEYCODE_DEL) {
        } else if (keyCode == KeyEvent.KEYCODE_BACK) {
        } else if (keyCode == KeyEvent.KEYCODE_ENTER) {
        } else {
            return super.dispatchKeyEvent(event);
        }
		return super.dispatchKeyEvent(event);
    }
    
	protected void onResume() 
	{
		Log.d("R", "onResume called");
		Music.resume();
		super.onResume();
		if(gameLoop != null) gameLoop.onResume();
		//glSurfaceView.onResume();
		
	}

	protected void onPause() 
	{
		Log.d("R", "onPause called");
		glSurfaceView.lockUI(null);
		Music.pause();
		super.onPause();
		if(gameLoop != null && !gameLoop.IsTerminated()) gameLoop.onPause();
		try { Thread.sleep(1000); } catch (InterruptedException e) {}
		//glSurfaceView.onPause();
		
	}
	@Override
	public void onBackPressed()
	{
		Log.d("R", "onBackPressed called");
		if(gameLoop != null && !gameLoop.IsTerminated()) gameLoop.onBack();
		UI.needRendering = true;
	}
	
	public void onConfigurationChanged(Configuration newConfig) 
	{
		  super.onConfigurationChanged(newConfig);
	}
	
	public static void MessageBox(final String msg)
	{
		MainProgram.currentActivity.runOnUiThread(new Runnable() 
		{
			public void run() 
			{
				AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(MainProgram.currentActivity);                      
			    dlgAlert.setMessage(msg);
			    dlgAlert.setTitle("Mortar System");              
			    dlgAlert.setPositiveButton("OK", null);
			    dlgAlert.setCancelable(false);
			    dlgAlert.create().show();	
			}
		});
	}
	
	public static void MessageBoxStrict(final String msg, final OnClickListener listener)
	{
		MainProgram.currentActivity.runOnUiThread(new Runnable() 
		{
			public void run() 
			{
				AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(MainProgram.currentActivity);                      
			    dlgAlert.setMessage(msg);
			    dlgAlert.setTitle("Mortar System");              
			    dlgAlert.setPositiveButton("OK", listener);
			    dlgAlert.setCancelable(false);
			    dlgAlert.create().show();	
			}
		});
	}
	
	public static void showBanner()
	{
		if(!ads) return;
		MainProgram.currentActivity.runOnUiThread(new Runnable() 
		{
			public void run() 
			{
				MainProgram.adView.setVisibility(View.VISIBLE);
				AdRequest adRequest = new AdRequest();
		        adView2.loadAd(adRequest);
			}
		});
		
	}
	
	public static void hideBanner()
	{
		if(!ads) return;
		MainProgram.currentActivity.runOnUiThread(new Runnable() 
		{
			public void run() 
			{
				MainProgram.adView.setVisibility(View.GONE);
			}
		});
	}
	
	
	
	public static void showBigAd()
	{
		if(!ads) return;
		if(adView2.isReady() && Math.random() > 0.999) 
		{
			adView2.show();
			
		}
		else Log.d("R", "bigad not ready");
	}
}